var loadFirstPage = {}
var LoadFirstPage = function () {
    var $_this = this;
    //设置全局STOMP协议对象
    this.stompClient = null;
    this.qrcodeImgDivIndex = null;
    this.tab = null;

    //页面初始化
    this.init = function () {
        var $, layer;
        window.model = {};
        window.metadata = {};
        window.sysUser = {};
        //清理缓存菜单
        sessionStorage.removeItem("menuList");

        //打开新窗口
        function addTab(_this) {
            $_this.tab.tabAdd(_this);
        }

        layui.config({
            base: "js/"
        }).extend({
            "bodyTab": "bodyTab"
        });
        layui.use(['bodyTab', 'form', 'element', 'layer', 'jquery'], function () {
            var form = layui.form,
                element = layui.element;
            $ = layui.$;
            layer = parent.layer === undefined ? layui.layer : top.layer;
            $_this.tab = layui.bodyTab({
                openTabNum: "50", //最大可打开窗口数量
                url : "json/navs.json" //获取菜单json地址
            });
            //获取登录用户
            layuiFunciton.ajaxRequest({
                url: '/ngu/sys/getCurrentUserInfo',
                method: 'get',
                success: function (data) {
                    if (data == null) {
                        window.location.href = "pages/login/login.html"
                    } else {
                        $(".adminName").text(data[0].name);

                    }
                }
            });
			//判断userInfo是否为空
            /*window.location.href = "pages/login/login.html"*/


            // //获取菜单
            // layuiFunciton.ajaxRequest({
            //     url: "/ngu/sysAuthority/querySysMenu",
            //     method: "post",
            //     data: {},
            //     success: function (returnDate) {
            //         if (returnDate.length <= 0) {
            //             layuiFunciton.warn("无权限！");
            //         }
            //         getMoble(returnDate);
            //         $_this.tab.render(returnDate);
            //         var jrr = JSON.stringify(returnDate);
            //         sessionStorage.setItem("menuList",jrr);
            //     }
            // });

            function getMoble(returnDate) {
                $.each(eval(returnDate), function (index, value, array) {
                    if (value.sysMetadataList.length > 0) {
                        window.metadata[value.id] = value.sysMetadataList;
                    }
                    if (value.children.length > 0) {
                        getMoble(value.children)
                    }
                });
            }

            //隐藏左侧导航
            $(".hideMenu").click(function () {
                if ($(".topLevelMenus li.layui-this a").data("url")) {
                    layer.msg("此栏目状态下左侧菜单不可展开"); //主要为了避免左侧显示的内容与顶部菜单不匹配
                    return false;
                }
                $(".layui-layout-admin").toggleClass("showMenu");
                //渲染顶部窗口
                $_this.tab.tabMove();
            });

            //手机设备的简单适配
            $('.site-tree-mobile').on('click', function () {
                $('body').addClass('site-mobile');
            });
            $('.site-mobile-shade').on('click', function () {
                $('body').removeClass('site-mobile');
            });

            // 添加新窗口
            $("body").on("click", ".layui-nav .layui-nav-item a:not('.mobileTopLevelMenus .layui-nav-item a')", function () {
                //如果不存在子级
                if ($(this).siblings().length == 0) {
                    addTab($(this));
                    $('body').removeClass('site-mobile'); //移动端点击菜单关闭菜单层
                }
                $(this).parent("li").siblings().removeClass("layui-nav-itemed");
            });


            //清除缓存
            $(".clearCache").click(function () {
                window.sessionStorage.clear();
                window.localStorage.clear();
                var index = layer.msg('清除缓存中，请稍候', {icon: 16, time: false, shade: 0.8});
                setTimeout(function () {
                    layer.close(index);
                    layer.msg("缓存清除成功！");
                }, 1000);
            });

            //刷新后还原打开的窗口
            if (cacheStr == "true") {
                if (window.sessionStorage.getItem("menu") != null) {
                    menu = JSON.parse(window.sessionStorage.getItem("menu"));
                    curmenu = window.sessionStorage.getItem("curmenu");
                    var openTitle = '';
                    for (var i = 0; i < menu.length; i++) {
                        openTitle = '';
                        if (menu[i].icon) {
                            if (menu[i].icon.split("-")[0] == 'icon') {
                                openTitle += '<i class="seraph ' + menu[i].icon + '"></i>';
                            } else {
                                openTitle += '<i class="layui-icon">' + menu[i].icon + '</i>';
                            }
                        }
                        openTitle += '<cite>' + menu[i].title + '</cite>';
                        openTitle += '<i class="layui-icon layui-unselect layui-tab-close" data-id="' + menu[i].layId + '">&#x1006;</i>';
                        element.tabAdd("bodyTab", {
                            title: openTitle,
                            content: "<iframe src='" + menu[i].href + "' data-id='" + menu[i].layId + "'></frame>",
                            id: menu[i].layId
                        });
                        //定位到刷新前的窗口
                        if (curmenu != "undefined") {
                            if (curmenu == '' || curmenu == "null") { //定位到后台首页
                                element.tabChange("bodyTab", '');
                            } else if (JSON.parse(curmenu).title == menu[i].title) { //定位到刷新前的页面
                                element.tabChange("bodyTab", menu[i].layId);
                            }
                        } else {
                            element.tabChange("bodyTab", menu[menu.length - 1].layId);
                        }
                    }
                    //渲染顶部窗口
                    $_this.tab.tabMove();
                }
            } else {
                window.sessionStorage.removeItem("menu");
                window.sessionStorage.removeItem("curmenu");
            }
        });
    }

    //显示系统版本
    this.showVersion = function(){
        layui.use('layer', function(){
            var layer = layui.layer;
            layer.alert("当前系统版本号：v4.0");
        });
    }


    //注册事件绑定
    this.registerEvent = function () {
        $('#sysVersion').click(function(){
            $_this.showVersion();
        });
    }
}


$(document).ready(function () {
    loadFirstPage = new LoadFirstPage();
    loadFirstPage.init();
    loadFirstPage.registerEvent();
});
//注销
function signOut() {
    layuiFunciton.ajaxRequest({
        url: '/ngu/sys/logout',
        method: 'get',
        success: function (data) {
            window.location.href = "pages/login/login.html";
        }
    });
}